using System;
using System.Runtime.InteropServices;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class ConsoleApplicationHelper
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleTextAttribute(
            IntPtr hConsoleOutput,
            CharacterAttributes wAttributes); /* declaring the setconsoletextattribute function*/

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetStdHandle(int nStdHandle); /*declaring the getstdhandle funtion to get thehandle that would be used in setConsoletextattribute function */

        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleTitle(String lpConsoleTitle);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="foreColor"></param>
        public static void SetForeColor(ConsoleTextForeColor foreColor)
        {
            IntPtr hOut; /* declaring varianle to get handle*/
            hOut = GetStdHandle(-11);/* -11 is sent for output device*/
            SetConsoleTextAttribute(hOut, (CharacterAttributes) foreColor);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="backColor"></param>
        public static void SetBackColor(ConsoleBackColor backColor)
        {
            IntPtr hOut; /* declaring varianle to get handle*/
            hOut = GetStdHandle(-11);/* -11 is sent for output device*/
            SetConsoleTextAttribute(hOut, (CharacterAttributes) backColor);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        public static void SetTitle(string title)
        {
            IntPtr hOut; /* declaring varianle to get handle*/
            hOut = GetStdHandle(-11);/* -11 is sent for output device*/
            SetConsoleTitle(title);
        }

        /// <summary>
        /// 
        /// </summary>
        public enum CharacterAttributes
        {

            /// <summary></summary>
            FOREGROUND_BLUE = 0x0001,
            /// <summary></summary>
            FOREGROUND_GREEN = 0x0002,
            /// <summary></summary>
            FOREGROUND_RED = 0x0004,
            /// <summary></summary>
            FOREGROUND_INTENSITY = 0x0008,
            /// <summary></summary>
            BACKGROUND_BLUE = 0x0010,
            /// <summary></summary>
            BACKGROUND_GREEN = 0x0020,
            /// <summary></summary>
            BACKGROUND_RED = 0x0040,
            /// <summary></summary>
            BACKGROUND_INTENSITY = 0x0080,
            /// <summary></summary>
            COMMON_LVB_LEADING_BYTE = 0x0100,
            /// <summary></summary>
            COMMON_LVB_TRAILING_BYTE = 0x0200,
            /// <summary></summary>
            COMMON_LVB_GRID_HORIZONTAL = 0x0400,
            /// <summary></summary>
            COMMON_LVB_GRID_LVERTICAL = 0x0800,
            /// <summary></summary>
            COMMON_LVB_GRID_RVERTICAL = 0x1000,
            /// <summary></summary>
            COMMON_LVB_REVERSE_VIDEO = 0x4000,
            /// <summary></summary>
            COMMON_LVB_UNDERSCORE = 0x8000
            
        }

        /// <summary>
        /// 
        /// </summary>
        public enum ConsoleTextForeColor
        {
            /// <summary></summary>
            FOREGROUND_BLUE = 0x0001,
            /// <summary></summary>
            FOREGROUND_GREEN = 0x0002,
            /// <summary></summary>
            FOREGROUND_RED = 0x0004,
            /// <summary></summary>
            FOREGROUND_INTENSITY = 0x0008
        }

        /// <summary>
        /// 
        /// </summary>
        public enum ConsoleBackColor
        {
            /// <summary></summary>
            BACKGROUND_BLUE = 0x0010,
            /// <summary></summary>
            BACKGROUND_GREEN = 0x0020,
            /// <summary></summary>
            BACKGROUND_RED = 0x0040,
            /// <summary></summary>
            BACKGROUND_INTENSITY = 0x0080
        }
    }
}
